<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of commonAction
 * 微信全局调用父类
 * @author xlp
 */
class commonAction extends action {

    //初始化全局信息

    function __construct() {
        parent::__construct();
        T('user/user');
        // //全局获取登录用户数据
        user::getLoginUser();
        // $this->_getUser();
    }

    /*
     * 获取当前用户
     */

    // private function _getUser() {
    //     if ($this->_checkLogin(true) || myCookie(C('system', 'checkLoginField'))) {
    //         return true;
    //     } elseif (getUserAgent() != 'unknown') {
    //         T('weixin/weixin.login');
    //         $loginApi = new loginApi();
    //         //跳转，获取微信身份信息
    //         $referInfo = array('url' => URL, 'aid' => $this->_getid('aid', 0), 'allReturn' => 1);
    //         mySession('referInfo', $referInfo);
    //         myCookie('refer_info', implode('|', $referInfo)); //加一道保险...
    //         jumpTo($loginApi->getOauthRedirect(U('weixin/openlogin/weixin'), 'base'));
    //     }
    //     return false;
    // }

    /*
     * 初始化头部信息
     * @param 无
     * @return
    */
    protected function _initHeader() {
        $this->assign('games' , $this->_getGameList());
        $this->assign('history_games' , $this->_getHistoryGames());
        $this->assign('letters' , C('letter'));
    }


    /*
     * 检查登录，用于必须登录的模块
     * @param 无
     * @return
     */

    protected function _checkLogin($returnBool = false) {
        //user::$userInfo['uid'] = 0;
        if (user::$userInfo['uid']) {
            return true;
        } else {
            return $returnBool ? false : showError('您未登录!');
        }
    }

   

    //检查访问来源
    protected function _checkUserAgent() {
        if (getUserAgent() == 'unknown') {
            if (isAjax()) {
                return $this->JsonReturn('403 Forbidden');
            } else {
                $this->_showSharePage();
            }
        }
    }

    

    /*
     * 调试函数
     */

    protected function debug() {
        if ($this->_getid('debug', 0)) {
            print_r($this->tVar);
            exit;
        }
    }


    /*
     * 检查HASH
     */

    protected function checkHash() {
        if (!formCheck()) {
            $this->JsonReturn('数据校验失败');
        } else {
            return true;
        }
    }

    
    /*
     * 获取游戏列表
     */
    protected function _getGameList() {
        $letters = C('letter');
        $objData = array();
        $where['status'] = 1;
        foreach ($letters as $k => $v) {
            $where['letter'] = $v;
            $objData[$v] = D('game')->where($where)->order('`sort`')->findAll();
        }
        //热门游戏
        unset($where['letter']);
        $where['is_hot'] = 1;
        $objData['hot']  = D('game')->where($where)->order('`sort`')->findAll();
        return $objData;
    }

    /*
     * 获取历史游戏搜索
     */
    protected function _getHistoryGames() {
        $historys = unserialize(myCookie('history'));
        $historyGames = array();
        if($historys) {
            foreach ($historys as $v) {
                $historyGames[] = M('game')->where(array('id'=>$v))->find();
            }
        }
        return $historyGames;
    }
}
